<!--
 * @Description: 佣金信息
 * @Author: xiao li
 * @Date: 2021-07-04 13:00:37
 * @LastEditTime: 2022-07-21 15:11:40
 * @LastEditors: xiao li
-->

<template>
  <div class="lb-shop-refund">
    <top-nav />
    <div class="page-main">
      <el-row class="page-search-form">
        <el-form
          @submit.native.prevent
          :inline="true"
          :model="searchForm"
          ref="searchForm"
        >
          <el-form-item label="姓名" prop="top_name">
            <el-input
              v-model="searchForm.top_name"
              placeholder="请输入姓名"
            ></el-input>
          </el-form-item>
          <el-form-item label="状态" prop="status">
            <el-select
              @change="getTableDataList(1)"
              v-model="searchForm.status"
              placeholder="请选择"
            >
              <el-option
                v-for="item in statusOptions"
                :key="item.value"
                :label="item.label"
                :value="item.value"
              ></el-option>
            </el-select>
          </el-form-item>
          <el-form-item>
            <lb-button
              size="medium"
              type="primary"
              icon="el-icon-search"
              style="margin-right: 5px"
              @click="getTableDataList(1)"
              >{{ $t('action.search') }}</lb-button
            >
            <lb-button
              size="medium"
              icon="el-icon-refresh-left"
              style="margin-right: 5px"
              @click="resetForm('searchForm')"
              >{{ $t('action.reset') }}</lb-button
            >
          </el-form-item>
        </el-form>
      </el-row>
      <el-table
        v-loading="loading"
        :data="tableData"
        :header-cell-style="{ background: '#f5f7fa', color: '#606266' }"
        style="width: 100%"
      >
        <el-table-column
          prop="id"
          label="ID"
          min-width="80"
          fixed
        ></el-table-column>
        <el-table-column
          prop="top_name"
          label="姓名"
          min-width="120"
        ></el-table-column>
        <el-table-column
          prop="nickName"
          label="来源"
          min-width="120"
        ></el-table-column>
        <el-table-column
          prop="order_code"
          width="150"
          label="系统订单号"
        ></el-table-column>
        <el-table-column
          prop="transaction_id"
          width="150"
          label="付款订单号"
        ></el-table-column>
        <!-- <el-table-column prop="status" label="类型">
          <template slot-scope="scope">
            {{ userType[scope.row.pay_type] }}
          </template>
        </el-table-column> -->
        <el-table-column prop="total_price" label="类型">
          <template slot-scope="scope">
            <span>{{ scope.row.type | typeText() }}</span>
          </template>
        </el-table-column>
        <el-table-column prop="status" label="状态">
          <template slot-scope="scope">
            {{ statusType[scope.row.status] }}
          </template>
        </el-table-column>
        <el-table-column
          prop="goods_info_text"
          min-width="300"
          label="提成比例"
        >
          <template slot-scope="scope">
            <div v-if="scope.row.type == 1">
              <div
                class="pb-sm"
                v-for="(citem, cindex) in scope.row.order_goods"
                :key="cindex"
                style="width: 250px"
              >
                <div class="flex-center pt-md">
                  <lb-image class="avatar radius-5" :src="citem.goods_cover" />
                  <div
                    class="flex-1 f-caption c-caption ml-md"
                    style="width: 180px"
                  >
                    <div class="flex-between">
                      <div
                        class="f-paragraph c-title ellipsis"
                        style="line-height: 1.2; margin-bottom: 4px"
                      >
                        {{ citem.goods_name }}
                      </div>
                    </div>
                    <div class="flex-y-center">
                      提成比例
                      <div class="c-warning ml-sm">{{ citem.balance }}%</div>
                    </div>
                    <div class="flex-between f-caption">
                      <div class="c-warning">¥{{ citem.true_price }}</div>
                      <div>x{{ citem.num }}</div>
                    </div>
                  </div>
                </div>
              </div>
            </div>
            <div v-else>{{ scope.row.balance }}%</div>
          </template>
        </el-table-column>
        <el-table-column prop="pay_price" label="订单总金额" min-width="120">
          <template slot-scope="scope"> ¥{{ scope.row.pay_price }}</template>
        </el-table-column>
        <el-table-column prop="cash" label="此单提成金额" min-width="120">
          <template slot-scope="scope"> ¥{{ scope.row.cash }}</template>
        </el-table-column>
        <el-table-column prop="create_time" min-width="120" label="时间">
          <template slot-scope="scope">
            <div>{{ scope.row.create_time | handleTime(1) }}</div>
            <div>{{ scope.row.create_time | handleTime(2) }}</div>
          </template>
        </el-table-column>
      </el-table>
      <lb-page
        :batch="false"
        :page="searchForm.page"
        :pageSize="searchForm.limit"
        :total="total"
        @handleSizeChange="handleSizeChange"
        @handleCurrentChange="handleCurrentChange"
      >
      </lb-page>
    </div>
  </div>
</template>

<script>
import moment from 'moment';
export default {
  data () {
    return {
      statusOptions: [
        {
          label: '全部',
          value: 0,
        },
        {
          label: '未入账',
          value: 1,
        },
        {
          label: '已入账',
          value: 2,
        },
      ],
      userType: {
        1: '会员提成',
        2: '技师提成',
      },
      statusType: {
        1: '未入账',
        2: '已入账',
      },
      loading: false,
      searchForm: {
        page: 1,
        limit: 10,
        top_name: '',
        status: 0,
      },
      tableData: [],
      total: 0,
      dialogRefund: false,
      refundId: '',
      refundMoney: '',
      refundTotalMoney: '',
      lockRefund: false,
    };
  },
  created () {
    this.getTableDataList();
  },
  methods: {
    resetForm (form) {
      this.$refs[form].resetFields();
      this.getTableDataList(1);
    },
    handleSizeChange (val) {
      this.searchForm.limit = val;
      this.handleCurrentChange(1);
    },
    handleCurrentChange (val) {
      this.searchForm.page = val;
      this.getTableDataList();
    },
    async getTableDataList (flag) {
      if (flag) this.searchForm.page = 1;
      this.loading = true;
      let { searchForm } = this;
      let { code, data } = await this.$api.shop.commList(searchForm);
      this.loading = false;
      if (code !== 200) return;
      this.tableData = data.data;
      this.total = data.total;
    },
    confirmDel (id) {
      this.$confirm(
        this.$t('tips.confirmDelete'),
        this.$t('tips.reminder'),
        {
          confirmButtonText: this.$t('action.comfirm'),
          cancelButtonText: this.$t('action.cancel'),
          type: 'warning',
        }
      )
        .then(() => {
          this.updateItem(id, -1);
        })
        .catch(() => { });
    },
    async updateItem (id, status) {
      this.$api.shop.commentUpdate({ id, status }).then((res) => {
        if (res.code === 200) {
          this.$message.success(
            this.$t(
              status === -1
                ? 'tips.successDel'
                : 'tips.successOper'
            )
          );
          if (status === -1) {
            this.searchForm.page =
              this.searchForm.page <
                Math.ceil((this.total - 1) / this.searchForm.limit)
                ? this.searchForm.page
                : Math.ceil(
                  (this.total - 1) / this.searchForm.limit
                );
            this.getTableDataList();
          }
        } else {
          if (status === -1) return;
          this.getTableDataList();
        }
      });
    },
  },
  filters: {
    handleTime (val, type) {
      let time =
        type === 1
          ? moment(val * 1000).format('YYYY-MM-DD')
          : type === 2
            ? moment(val * 1000).format('HH:mm:ss')
            : moment(val * 1000).format('YYYY-MM-DD HH:mm:ss');
      return time;
    },
    typeText (val) {
      let texts = ['分销', '代理商', '技师'];
      return texts[val - 1];
    },
  },
};
</script>

<style lang="scss" scoped>
.lb-shop-refund {
  .page-main {
    width: 100%;

    .el-select,
    .el-input-number,
    .el-input {
      width: 200px;
    }
  }
}

.none {
  display: none;
}
</style>
<style media="print">
.none {
  display: block;
}
</style>
